package com.controller.sale;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.dao.sys.SysDictMapper;
import com.model.sale.ContractVOModel;
import com.model.sys.SysDict;
import com.service.external.sap.SaleService;
import com.service.sale.ContractDraftService;

/**
 * 合同起草
 */
@Controller("contractDraftController")
@RequestMapping(value = "/api")
public class ContractDraftController {
	static final Logger logger = LoggerFactory.getLogger(ContractDraftController.class);

	@Autowired
	ContractDraftService contractDraftService;

	@Autowired
	SaleService saleService;

	@Autowired
	SysDictMapper sysDictMapper;

	/**
	 * 查询列表
	 *
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "sale/contract/draft/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		return contractDraftService.selectList(map);
	}

	/**
	 * 查询详情
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "sale/contract/draft/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String id) {
		return contractDraftService.selectInfor(id);
	}

	@RequestMapping(value = "sale/contract/draft/updateMode", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult updateMode(@RequestParam String id) {
		return contractDraftService.updateMode(id);
	}

	/**
	 * 保存询价
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "sale/contract/draft/save", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult save(@RequestBody ContractVOModel contractVOModel) {
		String longTerm = contractVOModel.getLongTerm();
		// 长约合同
		if ("1".equals(longTerm)) {
			return contractDraftService.saveLongTerm(contractVOModel);
		} else {
			return contractDraftService.save(contractVOModel);
		}

	}

	/**
	 * 根据参数从sap接口抓取数据（运费、包装费、基准价）
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "sale/contract/draft/getDataFormSAPByParams", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult getDataFormSAPByParams(@RequestBody Map<String, String> params) {
		return saleService.loadDataFormSap(params);
	}

	/**
	 * 删除询价
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "sale/contract/draft/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String id) {
		return contractDraftService.delete(id);
	}

	/**
	 * 导出
	 *
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "sale/contract/draft/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("合同起草-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		contractDraftService.export(map, response);

	}

	/**
	 * 撤回合同
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "sale/contract/draft/back", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult back(@RequestBody ContractVOModel contractVOModel) throws Exception {
		return contractDraftService.back(contractVOModel);
	}

	/**
	 * 撤回长约合同
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "sale/contract/draft/backLongTerm", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult backLongTerm(@RequestBody ContractVOModel contractVOModel) throws Exception {
		return contractDraftService.backLongTerm(contractVOModel);
	}

	/**
	 * 根据产品编码查询是否获取远期定价的基准价
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "sale/contract/draft/productCodeWhetherNeedGetForward", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult productCodeWhetherNeedGetForward(@RequestBody Map params) {
		if (CollectionUtils.isEmpty(params)) {
			return HttpResult.error("参数不得为空");
		}
		SysDict sysDict = sysDictMapper.queryDictByParentNameAndDictValue(params);
		if (null == sysDict) {
			return HttpResult.ok(null);
		} else {
			return HttpResult.ok(sysDict);
		}
	}

}
